package org.healthy.util.umeng;

import com.alibaba.fastjson.JSON;
import org.healthy.model.MessageNotice;
import org.healthy.util.umeng.android.AndroidBroadcast;
import org.healthy.util.umeng.android.AndroidCustomizedcast;
import org.healthy.util.umeng.android.AndroidUnicast;
import org.healthy.utils.pub.exception.ExceptionEnums;

/**
 * Created by qingshengzheng on 16/1/28.
 */
public class UmengSender {

    private String appkey = "567a6cbde0f55af8e800085b";
    private String appMasterSecret = "1v7zjgugazdmdeaq3waigxi7ng5qwuhe";
    private String timestamp = null;
    private UmengPushClient client = new UmengPushClient();
    private static UmengSender sender;

    private UmengSender(String key, String secret) {
        try {
            appkey = key;
            appMasterSecret = secret;
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static UmengSender getInstance() {
        if (sender == null)
            sender = new UmengSender("567a6cbde0f55af8e800085b", "1v7zjgugazdmdeaq3waigxi7ng5qwuhe");
        return sender;
    }

    /**
     * 向android某个固定用户发送通知
     *
     * @param notice
     * @throws Exception
     */
    public void sendAndroidSingleNotice(MessageNotice notice) {
        AndroidCustomizedcast customizedcast = null;
        try {
            customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);

            customizedcast.setAlias(notice.to_user_id.toString(), "yixiu");
            customizedcast.setTicker(notice.ticker);
            customizedcast.setTitle(notice.title);
            customizedcast.setText(notice.content);
            customizedcast.goAppAfterOpen();
            customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
            customizedcast.setProductionMode();
            client.send(customizedcast);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 向android所有用户发送广播
     *
     * @throws Exception
     */
    public void sendAndroidAllNotice(UmengNotice notice) {
        AndroidBroadcast broadcast = null;
        try {
            broadcast = new AndroidBroadcast(appkey, appMasterSecret);

            broadcast.setTicker(notice.ticker);
            broadcast.setTitle(notice.title);
            broadcast.setText(notice.text);
            broadcast.goAppAfterOpen();
            broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
            broadcast.setProductionMode();
            client.send(broadcast);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 向android固定用户发送自定义消息
     */
    public void sendAndroidSingleMsg(MessageNotice notice) {
        AndroidCustomizedcast broadcast = null;
        try {
            broadcast = new AndroidCustomizedcast(appkey, appMasterSecret);

            broadcast.setAlias(notice.to_user_id.toString(), "yixiu");
            broadcast.setDisplayType(AndroidNotification.DisplayType.MESSAGE);
            broadcast.setCustomField(JSON.toJSONString(notice));
            broadcast.setProductionMode();
            client.send(broadcast);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
